package com.lagou.task14;

import java.lang.reflect.Array;
import java.util.*;

public class CollectionTest {

    public static void main(String[] args) {
//        1.准备一个Collection并打印
//        Collection c1 = new Collection();//抽象类不能实例化

        //接口类型的引用指向实现类实现多态
        Collection c1 = new ArrayList();
        System.out.println("集合元素: " + c1);//[]
//        2。向集合中添加单个元素并打印
        boolean b1 = c1.add("one");
        boolean b2 = c1.add(String.valueOf(4));
        boolean b3 = c1.add(new Person(12,"liyulin"));
        //打印集合中的元素就是每个对象调用自己的toString方法
        System.out.println("集合元素: " + c1);//[one, two, Person{age=12, name='liyulin'}]
//        3。向集合中添加多个元素并打印
        Collection c2 = new ArrayList();
        c2.add("111");
        c2.add("tree");
        System.out.println("c2集合元素：" + c2);//[111, tree]
//        c1.addAll(c2);
//        System.out.println("c1集合元素：" + c1);//[one, two, Person{age=12, name='liyulin'}, 111, tree]
        //考点
        c2.add(c1);
        System.out.println("c1集合元素：" + c2);//[111, tree, [one, two, Person{age=12, name='liyulin'}]]

        System.out.println("---------------------------------------");
        //判断集合中是否包含指定元素,工作原理：Objects.equals(o, e)
        System.out.println(c1.contains(new String("one")));
        boolean b4 = c1.contains(new Person(12,"liyulin"));
        System.out.println(b4);

        System.out.println("---------------------------------------");
        //集合是否包含另一个集合的所有元素
        Collection c3 = new ArrayList();
        c3.add(String.valueOf(5));
        c3.add("one");
        System.out.println("c1 = " + c1);
        System.out.println("c3 = " + c3);
        System.out.println(c1.containsAll(c3));

        //计算两个集合的交集
        System.out.println(c3.retainAll(c1));;
        System.out.println("c1 = " + c1);
        System.out.println("c3 = " + c3);

        //删除对象
        Collection c4 = new ArrayList();
        c4.add(1);
        c4.add(1);
        c4.add(2);
        c4.add(3);
        c4.add(2);
        c4.add(5);
        System.out.println("c4 = " + c4);//c4 = [1, 1, 2, 3, 2, 5]
        c4.remove(1);
        System.out.println("c4 = " + c4);
        Collection c5 = new ArrayList();
        c5.add(1);
        c5.add(2);
        c5.add(3);
        c4.removeAll(c5);
        System.out.println("c4 = " + c4);//[5]

        System.out.println("---------------------");
        //集合中其他方法的打印
        System.out.println(c4.isEmpty());//false

        System.out.println(c4.size());//1
        c4.clear();
        System.out.println(c4);//[]

        System.out.println("---------------------");
        //集合和数组类型之间的转换
        Collection c6 = new ArrayList();
        c6.add(new Person(12,"liyulin"));
        c6.add(new Person(13,"wangwu"));
        c6.add(1);
        System.out.println("c6 = " + c6);
        Object[] ob1 = c6.toArray();
        Collection al1 = Arrays.asList(ob1);
        System.out.println(Arrays.toString(ob1));
        System.out.println(al1);




    }


    private static class Person {
        int age;
        String name;
        public Person() {
        }
        public Person(int age, String name) {
            setAge(age);
            setName(name);
        }
        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Person person = (Person) o;
            return age == person.age &&
                    Objects.equals(name, person.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(age, name);
        }
    }
}

